<?include_once('template.php');?>

<html>
	<?
		$draft = getDraft($_SESSION['team']->draftID);
		$teamOnClock = getPickingTeam($draft);
		echo getHeader();
	?>

<body>
	<?echo getToolbar($draft, $ie, 2);?>
	<div class="container-fluid">
		<?echo getNavBar($draft);?>
		<div class="content">
			<?echo getClockMessage($teamOnClock);?>

			
			<?
$allTeams = getAllTeamsInDraft($draft->id);
$teamID = $_GET['id'] ?  $_GET['id'] : $_SESSION['team']->id;
$currentTeam = $_SESSION['team'];
$dropdown = "<select class='team-select' name='team-select'>";
$dropdown = $dropdown."<option value=''>Select A Team...</option>";
foreach($allTeams as $t)
{
	$dropdown = $dropdown."<option value='".$t->id."'>".$t->name."</option>";
	if ($t->id == $teamID)
	{
		$currentTeam = $t;
	}
}
$dropdown = $dropdown."</select>";

echo "<div class='team-header'>";
	echo "<span class='teamName'>".$currentTeam->name." - Draft Order: ".$currentTeam->draftOrder."</span>";
	echo "<span class='team-selector'>".$dropdown."</span>";
echo "</div>";

echo getPlayerTable($teamID);

?>
		</div>
	</div>
<script type='text/javascript' src='teamswitch.js'></script>
</body>
</html>


<?

function getSimplePlayerTable($teamID)
{
	$players = getPlayersForTeam($teamID);
	$table = "<table class='player";
		$table = $ie ? $table." ie'>" : $table."'>";
		$table = $table."<thead>"; 
		$table = $table."<tr>";
			$table = $table."<th scope='col' class='col1'>PICK</th>";
			$table = $table."<th scope='col' class='col2'>&nbsp</th>";
			$table = $table."<th scope='col' class='col3'>NAME</th>";
			$table = $table."<th scope='col' class='col4'>POS</th>";
			$table = $table."<th scope='col' class='col5'>BYE</th>";
		$table = $table."</tr>";  
	$table = $table."</thead>";
	$table = $table."<tbody>";
	foreach ($players as $p)
	{
		$table = $table.$p->getDisplayFormat(true);
	}
	return $table;
}


function getPlayerTable($teamID)
{
	$players = getPlayersForTeam($teamID);

	if (!$players)
	{
		
	}
	$rb = array();
	$wr = array();
	$bench = array();

	foreach ($players as $p)
	{
		if ($p->position == 'qb')
		{
			if (isset($qb))
			{
				$bench[] = $p;
			}
			else
			{
				$qb = $p;
			}
		}
		else if ($p->position == 'rb')
		{
			if (count($rb) < 2)
			{
				$rb[] = $p;
			}
			else if (!isset($util))
			{
				$util = $p;
			}
			else 
			{
				$bench[] = $p;
			}
		}
		else if ($p->position == 'wr')
		{
			if (count($wr) < 2)
			{
				$wr[] = $p;
			}
			else if (!isset($util))
			{
				$util = $p;
			}
			else 
			{
				$bench[] = $p;
			}
		}
		else if ($p->position == 'te')
		{
			if (!isset($te))
			{
				$te = $p;
			}
			else if (!isset($util))
			{
				$util = $p;
			}
			else
			{
				$bench[] = $p;
			}
		}
		else if ($p->position == 'k')
		{
			if (isset($kicker))
			{
				$bench[] = $p;
			}
			else
			{
				$kicker = $p;
			}
		}
		else if ($p->position == 'd')
		{
			if (isset($def))
			{
				$bench[] = $p;
			}
			else
			{
				$def = $p;
			}
		}
	}

	$table = "<table class='player";
		$table = $ie ? $table." ie'>" : $table."'>";
		$table = $table."<thead>"; 
		$table = $table."<tr>";
			$table = $table."<th scope='col' class='col1'>PICK</th>";
			$table = $table."<th scope='col' class='col2'>&nbsp</th>";
			$table = $table."<th scope='col' class='col3'>NAME</th>";
			$table = $table."<th scope='col' class='col4'>POS</th>";
			$table = $table."<th scope='col' class='col5'>BYE</th>";
		$table = $table."</tr>";  
	$table = $table."</thead>";
	$table = $table."<tbody>";
	// foreach ($players as $p)
	// {
		// $table = $table.$p->getDisplayFormat(true);
	// }
	//QB
	if (isset($qb))
	{
		$table = $table.$qb->getTeamDisplayFormat('qb');
	}
	else
	{
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>QB</td><td>&nbsp</td></tr>";
	}
	//RB
	if (count($rb) == 0)
	{
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>RB</td><td>&nbsp</td></tr>";
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>RB</td><td>&nbsp</td></tr>";
	}
	else if (count($rb) == 1)
	{
		$table = $table.$rb[0]->getTeamDisplayFormat('rb');
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>RB</td><td>&nbsp</td></tr>";
	}
	else
	{
		$table = $table.$rb[0]->getTeamDisplayFormat('rb');
		$table = $table.$rb[1]->getTeamDisplayFormat('rb');
	}
	//WR
	if (count($wr) == 0)
	{
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>WR</td><td>&nbsp</td></tr>";
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>WR</td><td>&nbsp</td></tr>";
	}
	else if (count($wr) == 1)
	{
		$table = $table.$wr[0]->getTeamDisplayFormat('wr');
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>WR</td><td>&nbsp</td></tr>";
	}
	else
	{
		$table = $table.$wr[0]->getTeamDisplayFormat('wr');
		$table = $table.$wr[1]->getTeamDisplayFormat('wr');
	}
	//UTIl
	if (isset($util))
	{
		$table = $table.$util->getTeamDisplayFormat('rq/wr/te');
	}
	else
	{
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>RB/WR/TE</td><td>&nbsp</td></tr>";
	}
	//TE
	if (isset($te))
	{
		$table = $table.$te->getTeamDisplayFormat('te');
	}
	else
	{
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>TE</td><td>&nbsp</td></tr>";
	}
	//K
	if (isset($kicker))
	{
		$table = $table.$kicker->getTeamDisplayFormat('k');
	}
	else
	{
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>K</td><td>&nbsp</td></tr>";
	}
	//DEF
	if (isset($def))
	{
		$table = $table.$def->getTeamDisplayFormat('def');
	}
	else
	{
		$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>D</td><td>&nbsp</td></tr>";
	}
	//BENCH
	for ($i=0;$i<5; $i++)
	{
		if (isset($bench[$i]))
		{
			$table = $table.$bench[$i]->getTeamDisplayFormat('bench');
		}
		else
		{
			$table = $table."<tr><td>--</td><td>&nbsp</td><td>----</td><td>B</td><td>&nbsp</td></tr>";
		}
	}
	$table = $table."</tbody>";
	$table = $table."</table>";
	
	return $table;
}

?>